package join;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/*
 * map输出的key
 */
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class BeanKey implements WritableComparable<BeanKey> {
    private int AddreNo;
    private boolean isPrimary; // true：address false：person


    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(AddreNo);
        out.writeBoolean(isPrimary);

    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.AddreNo = in.readInt();
        this.isPrimary = in.readBoolean();

    }

    // partitioner执行时调用hashcode()方法和compareTo()方法
    // compareTo()方法作为shuffle排序的默认方法
    @Override
    public int hashCode() {
        return this.AddreNo; // 按AddreNo进行分组
    }

    //用于排序,将相同的AddressNo的地址表和人员表,将地址表放到首位
    @Override
    public int compareTo(BeanKey o) {
        if (this.AddreNo == o.getAddreNo()) { // 如果是同一个AddressNo的数据则判断是Person还是Address表
            if (this.isPrimary == o.isPrimary()) {  //如果属性相同属于同种类型的表,返回0
                return 0;
            } else {
                return this.isPrimary ? -1 : 1; // true表示Address表 返回更小的值,将排至values队首
            }
        } else {
            return this.AddreNo - o.getAddreNo() > 0 ? 1 : -1;  //按AddressNo排序
        }
    }

}

